import BaseUI from "../../../Core/BaseUI";

const {ccclass, property} = cc._decorator;

@ccclass
export default class LoadingCloud extends BaseUI {
    private cloudsNode = [];
    private curStatus = ""

    protected onLoad() {
        //当前闭合状态
        this.curStatus = "close";

        //记录当前位置
        this.node.children.forEach((node: cc.Node) => {
            let pos = 1  //飘逸方向 大于0 右边 小于0 左边
            if (node.name == "cloud-1") {
                pos = -1;
            }

            let targetX: number = 0; //最终飘逸位置
            let width: number = node.scale * node.width;

            //偏左往右
            if (node.x < 0 && pos > 0) {
                targetX = node.x * -1 + width
            }
            //偏左往左
            else if (node.x < 0 && pos < 0) {
                targetX = node.x - (width - node.x)
            }
            //偏右往左
            else if (node.x > 0 && pos < 0) {
                targetX = node.x * -1 + width
            }
            //偏右往右
            else if (node.x > 0 && pos > 0) {
                targetX = node.x + (width - node.x)
            }

            this.cloudsNode.push({
                x: node.x,
                w: width,
                targetX: targetX
            })
        })
    }

    public async onShow(data?: any): Promise<void> {
        console.log("LoadingCloud onshow")
    }

    public async onHide(): Promise<void> {
        console.log("LoadingCloud onhide")
    }

    public async showEffect() {

        //闭合状态先打开
        if (this.curStatus == "close") {
            //先把云复位到目标位置
            this.node.children.forEach((node: cc.Node, key) => {
                node.x = this.cloudsNode[key].targetX
                node.opacity = 0
            });

            this.curStatus = "open";
        }


        //动画到原位置
        let tweenArray = [];
        this.node.children.forEach((node: cc.Node, key) => {
            let asyncTween = new Promise<void>((resolve, reject) => {
                cc.tween(node)
                    .to(1, {x: this.cloudsNode[key].x,opacity: 255}, cc.easeIn(3.0)).
                    call(() => {
                        resolve();
                    }).start();
            });

            tweenArray.push(asyncTween)
        });

        await Promise.all(tweenArray);
        this.curStatus = "close";
    }

    public async hideEffect() {
        if (this.curStatus == "open") {
            //先把云复位到目标位置
            this.node.children.forEach((node: cc.Node, key) => {
                node.x = this.cloudsNode[key].x
                node.opacity = 255
            });

            this.curStatus = "close";
        }

        //动画到原位置
        let tweenArray = [];
        this.node.children.forEach((node: cc.Node, key) => {
            let asyncTween = new Promise<void>((resolve, reject) => {
                cc.tween(node)
                    .to(0.5, {x: this.cloudsNode[key].targetX,opacity:0}, cc.easeIn(3.0))
                    .call(() => {
                        resolve();
                    }).start();
            });

            tweenArray.push(asyncTween)
        });

        await Promise.all(tweenArray);
        this.curStatus = "open";
    }
}